package com.example.seabedmusic.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.seabedmusic.entity.Song;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class LocalMusicManager {
    private static final String TAG = "LocalMusicManager";
    private static final String PREF_NAME = "local_music_prefs";
    private static final String KEY_LOCAL_SONGS = "local_songs";

    private static LocalMusicManager instance;
    private SharedPreferences sharedPreferences;
    private Gson gson;

    private LocalMusicManager(Context context) {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        gson = new Gson();
    }

    public static synchronized LocalMusicManager getInstance(Context context) {
        if (instance == null) {
            instance = new LocalMusicManager(context);
        }
        return instance;
    }

    /**
     * 添加歌曲到本地音乐
     */
    public boolean addSongToLocal(Song song) {
        try {
            List<Song> localSongs = getLocalSongs();

            // 检查是否已存在
            for (Song existingSong : localSongs) {
                if (existingSong.getId().equals(song.getId()) ||
                        (existingSong.getTitle().equals(song.getTitle()) &&
                                existingSong.getArtist().equals(song.getArtist()))) {
                    return false; // 已存在
                }
            }

            // 设置歌曲为本地音乐
            song.setLocal(true);
            song.setFilePath("local://" + song.getId()); // 模拟本地文件路径

            localSongs.add(song);
            saveLocalSongs(localSongs);

            Log.d(TAG, "歌曲已添加到本地音乐: " + song.getTitle() + " - " + song.getArtist());
            return true;

        } catch (Exception e) {
            Log.e(TAG, "添加歌曲到本地音乐失败", e);
            return false;
        }
    }

    /**
     * 获取所有本地音乐
     */
    public List<Song> getLocalSongs() {
        String songsJson = sharedPreferences.getString(KEY_LOCAL_SONGS, "[]");
        Type listType = new TypeToken<List<Song>>(){}.getType();
        List<Song> songs = gson.fromJson(songsJson, listType);
        return songs != null ? songs : new ArrayList<>();
    }

    /**
     * 从本地音乐移除歌曲
     */
    public boolean removeSongFromLocal(Song song) {
        try {
            List<Song> localSongs = getLocalSongs();
            boolean removed = localSongs.removeIf(s ->
                    s.getId().equals(song.getId()) ||
                            (s.getTitle().equals(song.getTitle()) && s.getArtist().equals(song.getArtist()))
            );

            if (removed) {
                saveLocalSongs(localSongs);
                Log.d(TAG, "歌曲已从本地音乐移除: " + song.getTitle());
            }

            return removed;

        } catch (Exception e) {
            Log.e(TAG, "从本地音乐移除歌曲失败", e);
            return false;
        }
    }

    /**
     * 检查歌曲是否在本地音乐中
     */
    public boolean isSongInLocal(Song song) {
        List<Song> localSongs = getLocalSongs();
        for (Song localSong : localSongs) {
            if (localSong.getId().equals(song.getId()) ||
                    (localSong.getTitle().equals(song.getTitle()) &&
                            localSong.getArtist().equals(song.getArtist()))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 清空本地音乐
     */
    public void clearLocalSongs() {
        sharedPreferences.edit().remove(KEY_LOCAL_SONGS).apply();
        Log.d(TAG, "本地音乐已清空");
    }

    private void saveLocalSongs(List<Song> songs) {
        String songsJson = gson.toJson(songs);
        sharedPreferences.edit().putString(KEY_LOCAL_SONGS, songsJson).apply();
    }
}